package com.example.ccj.b.c.custom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.IntDef;
import androidx.annotation.Nullable;

import com.example.ccj.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 自定义选择控件，包含单选和多选的样式
 * isSelected设置是否选中
 * showText 选中的文本
 * isRight 这个选项是否正确
 * isCheck 这个选项是否检查对错，这个要设置为true  isRight 才会发挥作用
 * tag 选项的编号  A\B\C\D
 */
public class MNewRadioButton extends LinearLayout implements View.OnClickListener{

    public static final int SINGLE = 0;
    public static final int MULTIPLE = 1;


    long clickTime = 0;
    int checkInterval = 300;//两次点击间隔
    boolean onlyOne = false;

    OnClickListener onClickListenerN = new OnClickListener() {
        @Override
        public void onClick(View v) {
            //点击回调
            setOnClickSelected(!isSelected);
        }
    };

    @Override
    public void onClick(View v) {
        if (onlyOne) {
            if (onClickListenerN != null) onClickListenerN.onClick(v);
            setOnClickListener(null);
        } else if (!(System.currentTimeMillis() - clickTime < checkInterval)) {
            clickTime = System.currentTimeMillis();
            if (onClickListenerN != null) onClickListenerN.onClick(v);
        }
    }

    @IntDef({SINGLE,MULTIPLE,})
    @Retention(RetentionPolicy.SOURCE)
    public @interface OptionChoiceMode {}

    //当前选项模式，单选或者多选
    int mode = SINGLE;
    //选项显示的图标
    MImageView mImageView;
    //选项编号控件
    TextView optionTag;
    //选项文本控件
    MTextView optionText;
    //显示的选项文本
    CharSequence showText;
    //是否正确
    boolean isCorrect = false;
    //是否选中
    boolean isSelected;
    //是否检查对错，决定是否显示错误以及正确图标
    boolean isAutoCheck;
    //选项文字文本大小
    int textSize = 18;
    //选项编号
    CharSequence tag;

    IMRadioButtonListener2 radioButtonListener;

    public MNewRadioButton(Context context){
        super(context);
        init(context);
    }

    public MNewRadioButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MNewRadioButton);
        //选项类型
        mode = typedArray.getInteger(R.styleable.MRadioButton_m_rb_mode,0);
        //文本大小
        textSize = typedArray.getInteger(R.styleable.MRadioButton_m_rb_text_size,20);
        typedArray.recycle();
        init(context);
    }

    private void init(Context context){
        LinearLayout.inflate(context, R.layout.custom_m_radio_button,this);
        mImageView = findViewById(R.id.custom_m_radio_button_view);
        optionTag = findViewById(R.id.custom_m_radio_button_option_text);
        optionText = findViewById(R.id.custom_m_radio_button_option_content_text);
        optionText.setTextColor(getResources().getColor(R.color.m_radio_button_def_text_color));
        super.setOnClickListener(this);
    }

    /**
     * 点击后的处理逻辑
     * @param selected 点击后应改变的状态
     */
    private void setOnClickSelected(boolean selected) {
        //如果是单选项并且当前已是选中状态不做更改
        if (isSelected && mode == SINGLE) return;
        isSelected = selected;
        if (radioButtonListener != null) radioButtonListener.onCheckedChanged(this,isSelected);
        setState();
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
        setState();
    }

    /**
     * 点击后的按钮及文字显示状态逻辑控制
     */
    public void setState(){
        if (isSelected) {
            optionTag.setVisibility(GONE);
            optionText.setTextColor(getResources().getColor(R.color.m_radio_button_selected_text_color));
            if (mode == SINGLE)  mImageView.setImageResource(R.mipmap.single_option_choice_selected);
            if (mode == MULTIPLE)  mImageView.setImageResource(R.mipmap.multiple_selected);
        } else {
            optionTag.setVisibility(VISIBLE);
            optionText.setTextColor(getResources().getColor(R.color.m_radio_button_def_text_color));
            if (mode == SINGLE)  mImageView.setImageResource(R.mipmap.single_option_choice_def);
            if (mode == MULTIPLE)  mImageView.setImageResource(R.mipmap.multiple_select_def);
        }
    }

    /**
     * 设置选择后的按钮及文字状态
     */
    public void replaceShowState(){
        //检查后不能再次点击
        if (isCorrect) {
            if (!isSelected) {
                optionTag.setVisibility(GONE);
                optionText.setTextColor(getResources().getColor(R.color.m_radio_button_right_text_color));
                if (mode == SINGLE) mImageView.setImageResource(R.mipmap.single_option_choice_right);
                if (mode == MULTIPLE) mImageView.setImageResource(R.mipmap.multiple_select_right);
            }
        } else {
            if (isSelected) {
                optionTag.setVisibility(GONE);
                optionText.setTextColor(getResources().getColor(R.color.m_radio_button_wrong_text_color));
                if (mode == SINGLE) mImageView.setImageResource(R.mipmap.single_option_choice_wrong);
                if (mode == MULTIPLE) mImageView.setImageResource(R.mipmap.multiple_select_wrong);
            }
        }
    }

    public void piGai(){
        setReadOnly();
        if (isSelected) {
            if (!isCorrect) {
                optionTag.setVisibility(GONE);
                optionText.setTextColor(getResources().getColor(R.color.m_radio_button_wrong_text_color));
                if (mode == SINGLE) mImageView.setImageResource(R.mipmap.single_option_choice_wrong);
                if (mode == MULTIPLE) mImageView.setImageResource(R.mipmap.multiple_select_wrong);
            }
        }
    }

    public long getClickTime() {
        return clickTime;
    }

    public int getCheckInterval() {
        return checkInterval;
    }

    public boolean isOnlyOne() {
        return onlyOne;
    }

    public int getMode() {
        return mode;
    }

    public CharSequence getShowText() {
        return showText;
    }

    public boolean isCorrect() {
        return isCorrect;
    }

    @Override
    public boolean isSelected() {
        return isSelected;
    }

    public boolean isAutoCheck() {
        return isAutoCheck;
    }

    public int getTextSize() {
        return textSize;
    }

    @Override
    public CharSequence getTag() {
        return tag;
    }

    public IMRadioButtonListener2 getRadioButtonListener() {
        return radioButtonListener;
    }

    public MNewRadioButton setRadioButtonListener(IMRadioButtonListener2 radioButtonListener) {
        this.radioButtonListener = radioButtonListener;
        return this;
    }

    public MNewRadioButton setReadOnly(){
        super.setOnClickListener(null);
        return this;
    }

    public MNewRadioButton setMode(int mode) {
        this.mode = mode;
        return this;
    }

    public MNewRadioButton setShowText(CharSequence showText) {
        this.showText = showText;
        this.optionText.setMText(showText);
        return this;
    }

    public MNewRadioButton setCorrect(boolean correct) {
        isCorrect = correct;
        return this;
    }

    public MNewRadioButton setAutoCheck(boolean autoCheck) {
        isAutoCheck = autoCheck;
        return this;
    }

    public MNewRadioButton setTextSize(int textSize) {
        this.textSize = textSize;
        optionText.setTextSize(textSize);
        return this;
    }

    public MNewRadioButton setTag(CharSequence tag) {
        this.tag = tag;
        this.optionTag.setText(tag);
        return this;
    }



}
